package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.YxyKeChengUtil;
import com.china08.yxyapi.util.YxyResUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "v01", "教师课程-查看课时Video" })
@CrossOrigin
@RestController
@RequestMapping(value = "/api_yw")
public class YxyLessonTeaChapterGetItemController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;

    @Autowired
    private ElasticsearchTemplate et;

    @Autowired
    private YxyResUtil yxyResUtil;
    @Autowired
    private YxyKeChengUtil yxyKeChengUtil;
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @ApiOperation(value = "教师课程-查看章节课时视频")
    @RequestMapping(value = "/manage/tec/lessons/tea/getItem.v", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String userId = getProfile(request, response).getId();
        RespModel model = new RespModel();
        if (reqModel.clzss == 1) {
            YxyLessonsSchoolLessonItem item = yxyLessonsSchoolLessonItemRepository.findById(reqModel.itemId);
            Assert.notNull(item, "课时未找到");
            //
            // GetQuery getQuery = new GetQuery();
            // getQuery.setId(item.getVideoId());
            // ElasticYxyResResource elasticYxyResResource =
            // et.queryForObject(getQuery, ElasticYxyResResource.class);
            // Assert.notNull(elasticYxyResResource, "视频课找不到了");
            // YxyLessonsVideo video =
            // yxyLessonsVideoRepository.findById(item.getVideoId());
            //
            String lessonId = item.getLessonId();
            Integer itemOrderNum = item.getOrderNum();

            // 获取上一个 LessThan
            YxyLessonsSchoolLessonItem lastItem = yxyLessonsSchoolLessonItemRepository
                    .findTop1ByLessonIdAndOrderNumLessThanAndStatusNotOrderByOrderNumDesc(lessonId, itemOrderNum, 1);
            model.last = false;
            if (lastItem != null) {
                model.last = true;
                model.lastItemId = lastItem.getId();
                model.lastItemTitle = lastItem.getTitle();
                model.orderNum = lastItem.getOrderNum();
            }

            // 下一个
            YxyLessonsSchoolLessonItem nextItem = yxyLessonsSchoolLessonItemRepository
                    .findTop1ByLessonIdAndOrderNumGreaterThanAndStatusNotOrderByOrderNumAsc(lessonId, itemOrderNum, -1);
            model.next = false;
            if (nextItem != null) {
                model.next = true;
                model.nextItemId = nextItem.getId();
                model.nextItemTitle = nextItem.getTitle();
                model.orderNum = nextItem.getOrderNum();
            }
            //
            model.title = item.getTitle();
            model.intro = item.getIntro();
            // 视频播放路径
            model.preview = yxyKeChengUtil.genLessonItemPlayUrl(item.getId(), userId); // yxyResUtil.getPreviewUrl(item.getVideoId());
            // model.preview =
            // yxyResUtil.getPreviewUrl(elasticYxyResResource.getId());
        } else {
            YxyLessonsTopicLessonItem item = yxyLessonsTopicLessonItemRepository.findById(reqModel.itemId);
            Assert.notNull(item, "课时未找到");
            //
            GetQuery getQuery = new GetQuery();
            getQuery.setId(item.getVideoId());
            ElasticYxyResResource elasticYxyResResource = et.queryForObject(getQuery, ElasticYxyResResource.class);
            Assert.notNull(elasticYxyResResource, "视频课找不到了");

            //
            String lessonId = item.getLessonId();
            Integer itemOrderNum = item.getOrderNum();

            // 获取上一个 LessThan
            YxyLessonsTopicLessonItem lastItem = yxyLessonsTopicLessonItemRepository
                    .findTop1ByLessonIdAndOrderNumLessThanAndStatusNotOrderByOrderNumDesc(lessonId, itemOrderNum, 1);
            model.last = false;
            if (lastItem != null) {
                model.last = true;
                model.lastItemId = lastItem.getId();
                model.lastItemTitle = lastItem.getTitle();
                model.orderNum = lastItem.getOrderNum();
            }

            // 下一个
            YxyLessonsTopicLessonItem nextItem = yxyLessonsTopicLessonItemRepository
                    .findTop1ByLessonIdAndOrderNumGreaterThanAndStatusNotOrderByOrderNumAsc(lessonId, itemOrderNum, -1);
            model.next = false;
            if (nextItem != null) {
                model.next = true;
                model.nextItemId = nextItem.getId();
                model.nextItemTitle = nextItem.getTitle();
                model.orderNum = nextItem.getOrderNum();
            }
            //
            model.title = item.getTitle();
            model.intro = item.getIntro();
            model.preview = yxyResUtil.getPreviewUrl(elasticYxyResResource.getId());
        }
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String itemId;
        public Integer clzss; // 1教师 2专题

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(itemId, null), "章节ID不能为空");
        }
    }

    protected static class RespModel {
        public String preview;
        public String title;
        public String intro;
        public Integer orderNum;

        // 是否拥有上一个
        public boolean last;
        public String lastItemId;
        public String lastItemTitle;

        // 是否拥有下一个
        public boolean next;
        public String nextItemId;
        public String nextItemTitle;
    }
}
